const patrolComment = require("../models/patrolCommentModel");
const catchAsync = require("../utils/catchAsync");
const AppError = require("../utils/appError");

exports.createComment = catchAsync(async (req, res, next) => {
  const data = await patrolComment.create(req.body);
  if (!data) {
    res.status(200).json({
      status: "fail",
      data,
    });
  }
  res.status(201).json({
    status: "success",
    data,
  });
});
exports.deleteComment = catchAsync(async (req, res, next) => {
  const data = await patrolComment.findOneAndDelete({ _id: req.params.id });

  if (!data) {
    return next(new AppError("该巡课记录不存在", 404));
  }
  res.status(204).json({
    status: "success",
    data,
  });
});

exports.getPatrolMessage = catchAsync(async (req, res) => {
  let patrolMessage = await patrolComment.find(req.query)
    .populate({
      path: "lessonId", select: "course_id",
      populate: {
        path: "course_id", select: "name course_type",
      },
    })
    .populate({
      path: "lessonId", select: "teacher_id",
      populate: {
        path: "teacher_id", select: "name",
      },
    })
    .populate({
      path: "lessonId", select: "teacher_id",
      populate: {
        path: "teacher_id", select: "name",
      },
    })
  if (!patrolMessage) {
    res.json({
      status: "fail",
      message: "评课信息为空!"
    })
    return;
  }
  res.json({
    status: "success",
    patrolMessage
  })
})
